/**
 * Created by sicongdu on 17-11-3.
 */
var md5 = require("../../../md5").md5
var mongoose = require('mongoose');

const express = require('express');
var mark = express.Router();

var fs = require('fs');
var path = require("path");

var err = {
  error_no: 2,
  error_msg: "内部错误，请联系杜思聪～"
}

//release an exam
mark.post("/release_exam", function (req, res, next) {
  var exam = require("./mark_model").exam

  exam.create({
    name: req.body.name,
    start_time: req.body.start_time,
    stop_time: req.body.stop_time,
    address: req.body.address,
    teachers: req.body.teachers,
    classes: req.body.classes,
    status: req.body.status, //0：未开始 1: 签到  2：打分 3：结束
    student: req.body.student,
    score_way: req.body.score_way, // "score" or "level"
    levels_num: req.body.levels_num,
    detail: req.body.detail
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        exam: doc
      })
    }
  })

});

//get details of an exam
mark.post("/get_details_of_an_exam", function (req, res, next) {
  var exam = require("./mark_model").exam

  exam.findOne({
    _id: mongoose.Types.ObjectId(req.body.id)
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        exam: doc
      })
    }
  })

});

//update details of an exam
mark.post("/update_details", function (req, res, next) {
  var exam = require("./mark_model").exam

  exam.update({
    _id: mongoose.Types.ObjectId(req.body.exam_id)
  }, {
    $set: {
      name: req.body.name,
      start_time: req.body.start_time,
      stop_time: req.body.stop_time,
      address: req.body.address,
      teachers: req.body.teachers,
      classes: req.body.classes,
      status: req.body.status, //0：未开始 1: 签到  2：打分 3：结束
      student: req.body.student,
      score_way: req.body.score_way, // "score" or "level"
      levels_num: req.body.levels_num,
      detail: req.body.detail
    }
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        exam: doc
      })
    }
  })

});

//sign up as a group
mark.post("/sign", function (req, res, next) {
  var group = require("./mark_model").group
  var status = req.body.status

  if (status == "0") {
    group.create({
      exam_id: req.body.exam_id, //id2
      name: req.body.name,
      members: req.body.members,
      is_marked: "0"
    }, (error, doc) => {
      if (error) {
        res.send(err)
      } else {
        res.send({
          error_no: 0,
          error_msg: "ok",
          group: doc
        })
      }
    })
  } else if (status == "1") {
    var where = {
      _id: req.body._id
    }

    var update = {
      $set: {
        exam_id: req.body.exam_id,
        name: req.body.name,
        members: req.body.members,
      }
    }
    group.update(where, update, (error) => {
      "use strict";
      if (error) {
        res.send(err)
      } else {
        res.send({
          error_no: 0,
          error_msg: "ok"
        })
      }
    })
  }


});

//give grades for a group
mark.post("/grade", function (req, res, next) {
  var grade = require("./mark_model").grade

  grade.create({
    exam_id: req.body.exam_id,
    studentId: req.body.studentId,
    name:req.body.name,
    grades: req.body.grades.split(",")
  }, (error, doc) => {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        grade: doc
      })

    }
  })

});

//get all the exams
mark.post("/get_all_the_exams", function (req, res, next) {
  var exam = require("./mark_model").exam

  exam.find({}, function (error, docs) {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        exams: docs
      })
    }
  });

});

//get current exam
mark.post("/get_current_exam", function (req, res, next) {
  var exam = require("./mark_model").exam

  var now = new Date()
  var today = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate()

  exam.find({}).sort({'_id':-1}).limit(1).exec( function (error, docs) {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        exam: docs[0]
      })
    }
  });

});

//get current student
mark.post("/get_current_student", function (req, res, next) {
  var grade = require("./mark_model").grade

  grade.find({
    name: req.body.name,
    studentId: req.body.studentId,
    exam_id: req.body.exam_id
  }).sort({'_id':-1}).limit(1).exec(function (error, docs) {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        grade: docs[0] || {
          grades: ["","","","","","",""],
        }
      })
    }
  })

});

//get current class
mark.post("/get_current_class", function (req, res, next) {
  var groups = require("./mark_model").group
  var current = req.body.current + ""
  // res.send(`/^${current}-.*/`)

  groups.find({
    name: {$regex: eval(`/^${current}-.*/`)},
    exam_id: req.body.exam_id
  }, function (error, docs) {
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        groups: docs
      })
    }
  });

});


//get details for a group sign
mark.post("/get_sign_detail", function (req, res, next) {
  var group = require("./mark_model").group
  var studentNo = req.body.studentNo

  group.find({
    exam_id: req.body.exam_id
  }, function (error, docs) {
    if (error) {
      res.send(err)
    } else {
      var groups = JSON.parse(JSON.stringify(docs))

      for (var i = 0; i < groups.length; i++) {
        if (groups[i].members.split("/").includes(studentNo)) {
          res.send({
            error_no: 0,
            error_msg: "ok",
            group: groups[i]
          })
          return
        }
      }

      res.send({
        error_no: 1,
        error_msg: "not signed"
      })
    }
  });

});

//get name by id
mark.post("/get_name_by_id", function (req, res, next) {
  var getUserInfo = require("../getUserInfo").getUserInfo

  getUserInfo(req.body.kind, req.body.id, res)

});

//get all groups
mark.post("/get_all_groups", function (req, res, next) {
  var groups = require("./mark_model").group

  groups.find({
    exam_id: req.body.exam_id
  }, (error, docs) => {
    "use strict";
    if (error) {
      res.send(err)
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        groups: docs
      })
    }
  })
});

module.exports = mark;
